﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Lottery.Herper;

namespace Lottery.MediaData.UI {
    public partial class CombineLineForm : Form {
        private List<string> combines;

        public CombineLineForm(List<string> combines)
            : this() {

            this.SetDialogStyle();
            editFrom1.JumpToNext = editTo1;
            editTo1.JumpToNext = editFrom2;
            editFrom2.JumpToNext = editTo2;
            editTo2.JumpToNext = buttonOK;

            this.combines = combines;

            var a = combines.ToArray();
            editFrom1.Items.AddRange(a);
            editTo1.Items.AddRange(a);
            editFrom2.Items.AddRange(a);
            editTo2.Items.AddRange(a);
        }

        private CombineLineForm() {
            InitializeComponent();
        }

        private int from1;
        private int to1;
        private int from2;
        private int to2;

        /// <summary>
        /// 区域1 小数
        /// </summary>
        public int From1 {
            get { return from1; }
            set {
                from1 = value;
                editFrom1.SelectedIndex = editFrom1.Items.IndexOf(from1.ToString());
            }
        }

        /// <summary>
        /// 区域2 大数
        /// </summary>
        public int To1 {
            get { return to1; }
            set {
                to1 = value;
                editTo1.SelectedIndex = editTo1.Items.IndexOf(to1.ToString());
            }
        }

        /// <summary>
        /// 区域2 小数
        /// </summary>
        public int From2 {
            get { return from2; }
            set {
                from2 = value;
                editFrom2.SelectedIndex = editFrom2.Items.IndexOf(from2.ToString());
            }
        }

        /// <summary>
        /// 区域2 大数
        /// </summary>
        public int To2 {
            get { return to2; }
            set {
                to2 = value;
                editTo2.SelectedIndex = editTo2.Items.IndexOf(to2.ToString());
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e) {
            // 取消

            DialogResult = DialogResult.Cancel;
        }

        private void buttonOK_Click(object sender, EventArgs e) {
            // 确定

            var fromIndex1 = editFrom1.SelectedIndex;
            var fromIndex2 = editFrom2.SelectedIndex;
            var toIndex1 = editTo1.SelectedIndex;
            var toIndex2 = editTo2.SelectedIndex;

            if (fromIndex1 == -1) {
                this.ShowInfo("没有输入区域1的小数");
                editFrom1.Focus();
                return;
            }

            if (toIndex1 == -1) {
                this.ShowInfo("没有输入区域1的大数");
                editTo1.Focus();
                return;
            }

            if (fromIndex2 == -1) {
                this.ShowInfo("没有输入区域2的小数");
                editFrom2.Focus();
                return;
            }

            if (toIndex2 == -1) {
                this.ShowInfo("没有输入区域2的大数");
                editTo2.Focus();
                return;
            }

            // 判断：区域1的"小数"是否不大于"大数"
            if (fromIndex1 > toIndex1) {
                this.ShowInfo("区域1的小数不能大于大数");
                editFrom1.Focus();
                return;
            }

            // 判断：区域2的"小数"是否不大于"大数"
            if (fromIndex2 > toIndex2) {
                this.ShowInfo("区域2的小数不能大于大数");
                editFrom2.Focus();
                return;
            }

            // 判断：区域1和区域2是否重叠
            if (fromIndex1 >= fromIndex2 && fromIndex1 <= toIndex2) {
                this.ShowInfo("区域1和区域2重叠");
                editFrom1.Focus();
                return;
            }

            From1 = Convert.ToInt32(editFrom1.Text);
            To1 = Convert.ToInt32(editTo1.Text);
            From2 = Convert.ToInt32(editFrom2.Text);
            To2 = Convert.ToInt32(editTo2.Text);

            DialogResult = DialogResult.OK;
        }
    }
}
